/**
 * Created by jiaaobo on 16/11/29.
 */

import Util from "util";
import API  from "api";
import CONST from  "const";
import OrderCodeComponent from  "./../view/OrderCodeComponent";
import CDSev from "../../../common/util/seckill";
let currentTimerObj;

export default {
  name: "orderDetailMain",
  data(){
    return {
      orderDetail : {},
      isShow : false,
      seckillMsg : ''
    }
  },

  computed: {
    getStarTime: function () {
      return new Date(this.orderDetail.startTime).Format('MM月dd日 hh:ss');
    },

    getEndTime: function () {
      return new Date(this.orderDetail.endTime).Format('hh:ss');
    },
    getCancelTime: function () {
      return new Date(this.orderDetail.modifyDate).Format('MM月dd日 hh:ss');
    }
  },

  mounted: function () {
    let id =  this.$route.params.id;
    if(!id){
      Util.UI.toast("请输入ID..");
      return;
    }
    this.getOrderDetail(id);


    
    //显示分享
    Util.WX.toggleMenu(true,'spread');
  },

  beforeDestroy : function(){
    if(currentTimerObj)
      CDSev.clearTimer(currentTimerObj.id);
  },

  methods: {
    showLocation : function(){
      Util.WX.openLocation(this.orderDetail.lat,this.orderDetail.lng);
    },
    cancelOrder : function(){
      API.biz.cancelAppoint(this.orderDetail.appointId,{hasLoading : true})
        .then(res=>{
          Util.UI.toast("取消预约成功!");
          window.getRouter().replace({'path' : '/order/my'});
        });
    },

    onPay : function(){
      console.log("支付...");
      Util.UI.loading("show");
      API.wx.commitOrder(this.orderDetail.orderNo)
        .then(res=>{
          let wxParams = {
            timestamp : res.bizData.paymap.timeStamp,
            nonceStr: res.bizData.paymap.nonceStr, // 支付签名随机串，不长于 32 位
            package: res.bizData.paymap.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
            signType: res.bizData.paymap.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
            paySign: res.bizData.paymap.paySign
          };
          Util.WX.wxPay(wxParams)
            .then((res)=>{
              Util.UI.toast("支付成功!");
              Util.UI.loading("hide");
              window.getRouter().replace({'path' : '/order/my'});
            },(err)=>{
              Util.UI.loading("hide");
              if(res.state == 500){
                Util.UI.toast("支付失败!");
                console.log(res);
              }
            });
        },(err)=>{
          Util.UI.loading("hide");
        });

    },

    getOrderDetail : function(id){
      API.biz.queryAppointDetailInfo(id,{hasLoading : true})
        .then(res=>{

            this.orderDetail = res.bizData;

            //测试已支付 未开始
            // this.orderDetail.state = 1;

            //已取消 超时取消
            //  this.orderDetail.state = 2;
            // this.orderDetail.state = 5;

            //待支付
           //this.orderDetail.state = 0;
           //this.orderDetail.createDate =  1483431010000;

            //支付倒计时
            if(this.orderDetail.state == 0){
                //倒计时
                //创建一个时间timer
                let time =  this.orderDetail.createDate  + 10 * 60 * 1000;
                currentTimerObj = CDSev.createTimer(time,(date)=>{
                  this.seckillMsg = date;
                },(date)=>{
                  console.log("结束...");
                  //超时
                  this.orderDetail.state = 2;
                },"detailTimer");
            }

            //已结束(正常)
            //this.orderDetail.state = 3;
            //已结束(超时)
            // this.orderDetail.state = 4;

            if(this.orderDetail.state == 3 || this.orderDetail.state == 4){
              // this.userList = res.bizData.userInfo;
              // this.userList = [
              //   {"userName" : "小宁","userIcon" : "http://static.imzhiliao.com/1472217176084rHQ289qAaj.jpg"},
              //   {"userName" : "小宁","userIcon" : "http://static.imzhiliao.com/1472217176084rHQ289qAaj.jpg"},
              //   {"userName" : "小宁","userIcon" : "http://static.imzhiliao.com/1472217176084rHQ289qAaj.jpg"},
              //   {"userName" : "小宁","userIcon" : "http://static.imzhiliao.com/1472217176084rHQ289qAaj.jpg"}
              // ];
            }


          this.isShow = true;
        });
    }

  },

  components: {
    OrderCodeComponent
  }
}
